package com.servlet;

import com.dao.imp.FindGoodsDaoImp;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/11/28 14:29
 **/

@WebServlet(name = "GetCheckedCartServlet", value = "/GetCheckedCartServlet")
public class GetCheckedCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("userId").toString());
        String sql = "SELECT * FROM shopingchart WHERE user_id="+user_id+" AND isChecked=1" ;

        FindGoodsDaoImp findGoodsDaoImp = new FindGoodsDaoImp();
        List<Map> userShoppingCartList = findGoodsDaoImp.findGoods(sql);
        List goodList = new ArrayList();
        for (int i = 0; i < userShoppingCartList.size(); i++) {
            int good_id = Integer.parseInt(userShoppingCartList.get(i).get("good_id").toString());
            goodList.add(findGoodsDaoImp.findGoodById(good_id));
//            System.out.println(userShoppingCartList.get(i).get("good_id"));
        }

        List data = new ArrayList();
        data.add(goodList);
        data.add(userShoppingCartList);
        JSONArray json = JSONArray.fromObject(data);
        response.getWriter().print(json);
    }
}
